package org.eae.eldf.webapp.base;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import org.eae.eldf.framework.editor.DateEditor;
import org.eae.eldf.framework.pager.Pagination;
import org.eae.eldf.webapp.common.bean.ResultList;
import org.eae.eldf.webapp.vo.PageListVo;

public abstract class AbstractController<T> {
	
	protected static final Logger logger = LoggerFactory.getLogger(AbstractController.class);
	
	protected static final String USER_SESSION_NAME = "CurrentUser";
	
	@InitBinder
	protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
	    //对于需要转换为Date类型的属性，使用DateEditor进行处理
	    binder.registerCustomEditor(Date.class, new DateEditor());
	}
	
	public Pagination<T> initPagination(HttpServletRequest request){
		Pagination<T> pager = new Pagination<T>();
		if(StringUtils.isNotBlank(request.getParameter("page"))){
			pager.setCurrentPage(Integer.parseInt(request.getParameter("page")));
		}else{
			pager.setCurrentPage(1);
		}
		if(StringUtils.isNotBlank(request.getParameter("rows"))){
			pager.setNumPerPage(Integer.parseInt(request.getParameter("rows")));
		}else{
			pager.setNumPerPage(10);
		}
		String iSortingCols = request.getParameter("sidx");
		if(StringUtils.isNotBlank(iSortingCols)){//排序列
			pager.setOrderBy(iSortingCols);
			pager.setSort(request.getParameter("sord"));
		}
		return pager;
	}
	
	public PageListVo<T> pageListJson(Pagination<T> pager){
		PageListVo<T> res = new PageListVo<T>();
		res.setPage(pager.getCurrentPage());
		res.setTotal(pager.getTotalPages());
		res.setRows(pager.getResultList());
		res.setRecords(pager.getTotalRows());
		return res;
	}
	
	
	public Pagination<T> initPager(HttpServletRequest request,HttpServletResponse response){
		Pagination<T> pager = new Pagination<T>();
		if(StringUtils.isNotBlank(request.getParameter("iDisplayStart")))
			pager.setCurrentPage(Integer.parseInt(request.getParameter("iDisplayStart")));
		else
			pager.setCurrentPage(1);
		if(StringUtils.isNotBlank(request.getParameter("iDisplayLength")))
			pager.setNumPerPage(Integer.parseInt(request.getParameter("iDisplayLength")));
		else
			pager.setNumPerPage(10);
		String iSortingCols = request.getParameter("iSortingCols");
		if(StringUtils.isNotBlank(iSortingCols)){//排序列
			String mDataProp_ = request.getParameter("mDataProp_"+iSortingCols);
			String sSortDir_0 = request.getParameter("sSortDir_0");
			pager.setOrderBy(mDataProp_);
			pager.setSort(sSortDir_0);
		}
		String sortingAscDesc = request.getParameter("sorting_asc_desc");
		String sorting_column_order = request.getParameter("sorting_column_order");
		if(StringUtils.isNotBlank(sortingAscDesc) && StringUtils.isNotBlank(sorting_column_order)){
			pager.setOrderBy(sorting_column_order);
			pager.setSort(sortingAscDesc);
		}
		return pager;
	}
	
	
	protected Map<String,Object> makeReadonly(Map<String,Object> field){
		field.put("readonly", true);
		return field;
	}


	public Pagination<T> newPaginationInstance(org.eae.eldf.webapp.common.bean.Pagination pagination){
		Pagination<T> pager = new Pagination<T>();
		pager.setCurrentPage(pagination.getCurrentPage());
		pager.setNumPerPage(pagination.getCount());
		return pager;
	}
	
	public ResultList<T> outPageArrayList(Pagination<T> pager,org.eae.eldf.webapp.common.bean.Pagination pagination){
		ResultList<T> res = new ResultList<T>();
		res.setResultList(pager.getResultList());
		pagination.setPages(pager.getTotalPages());
		pagination.setTotalItems(pager.getTotalRows());
		pagination.setCurrentPage(pager.getCurrentPage());
		res.setPagination(pagination);
		return res;
	}


	
}